package com.itcast.springboot.waste_recovery.service.impl;

import com.itcast.springboot.waste_recovery.domain.User;
import com.itcast.springboot.waste_recovery.domain.Withdrawal;
import com.itcast.springboot.waste_recovery.mapper.UserMapper;
import com.itcast.springboot.waste_recovery.mapper.WithdrawalMapper;
import com.itcast.springboot.waste_recovery.service.WithdrawalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;

/**
 * <p>
 * 提现表 服务实现类
 * </p>
 *
 * @author 小破烂
 * @since 2020-08-23
 */
@Service
public class WithdrawalServiceImpl extends ServiceImpl<WithdrawalMapper, Withdrawal> implements WithdrawalService {
    @Resource
    WithdrawalMapper withdrawalMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public Integer Withdrawal(Integer userId, Float withdrawalAmount) {
        Withdrawal withdrawal = new Withdrawal();
        withdrawal.setWithdrawalUserId(userId);
        withdrawal.setWithdrawalAmount(withdrawalAmount);
        Integer result = -1;
        User user = userMapper.selectById(withdrawal.getWithdrawalUserId());
        //要提现为金额的回收币数量，10回收币 = 1元
        Integer withdrawalUserMoney = (int)(withdrawal.getWithdrawalAmount() * 10);
        //用户现有的回收币>=要提现的回收币
        if (user.getUserMoney() >= withdrawalUserMoney){
            user.setUserMoney(user.getUserMoney() - withdrawalUserMoney);
            userMapper.updateById(user);
            result = withdrawalMapper.insert(withdrawal);
            if (result > 0){
                result = user.getUserMoney();
            }
        }
        return result;
    }
}
